package com.example.smart.ui.activity.login

import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.StyleSpan
import android.text.style.UnderlineSpan
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.utils.sp.SpKey
import com.example.lib.utils.sp.SpUtils
import com.example.lib.utils.statusbar.StatusBarUtils
import com.example.smart.R
import com.example.smart.base.ui.activity.BaseActivity
import com.example.smart.databinding.ActivityWelcomeBinding
import com.example.smart.dialog.ChooseLanguageDialog
import com.example.smart.utils.DisplayUtils

class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {

    private val mDialog by lazy {
        ChooseLanguageDialog(this@WelcomeActivity)
    }

    override fun layoutID() = R.layout.activity_welcome

    override fun initView() {
        StatusBarUtils.setStateAndNavigationBar(this@WelcomeActivity,
            statusBarColor = com.example.lib.R.color.translate)

        val text = getString(R.string.welcome_text)
        with(SpannableStringBuilder(text)) {
            val termsIndex = text.indexOf("APP")
            setSpan(StyleSpan(Typeface.BOLD), termsIndex, termsIndex + 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
            binding.Message.movementMethod = LinkMovementMethod.getInstance()
            binding.Message.text = this
        }
    }

    override fun initListener() {
        binding.root.setOnDoubleClickListener {
            mDialog.show()
        }

        binding.Message.setOnDoubleClickListener {
            mDialog.show()
        }

        mDialog.onClick = {
            mDialog.dismiss()
            if (it == ChooseLanguageDialog.MENG) {
                DisplayUtils.type = 1
            } else {
                DisplayUtils.type = 0
            }
            if (SpUtils.getBoolean(SpKey.IS_LOGIN, false)) {
                DisplayUtils.toMain(this@WelcomeActivity)
            } else {
                DisplayUtils.toLoginChoose(this@WelcomeActivity)
            }
        }
    }
}